-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathConnect-SCP.ps1
108 lines (84 loc) · 3.19 KB
/
Connect-SCP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
$Metadata = @{
Title = "Connect SCP Session"
Filename = "Connect-SCP.ps1"
Description = ""
Tags = "powershell, remote, session, scp"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-05-17"
LastEditDate = "2014-01-22"
Version = "3.1.1"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Connect-SCP{
<#
.SYNOPSIS
Remote management for scp sessions.
.DESCRIPTION
Starts a scp session with the parameters from the remote config file.
.PARAMETER Name
Servername (Key or Name from the remote config file for preconfiguration).
.PARAMETER User
Username (overwrites remote config file parameter).
.PARAMETER Port
Port (overwrites remote config file parameter).
.PARAMETER PrivatKey
PrivatKey (overwrites remote config file parameter).
.EXAMPLE
Connect-SCP -Name firewall
#>
#--------------------------------------------------#
# Parameter
#--------------------------------------------------#
param (
[parameter(Mandatory=$true)]
[string[]]
$Name,
[parameter(Mandatory=$false)]
[string]$User,
[parameter(Mandatory=$false)]
[int]$Port,
[parameter(Mandatory=$false)]
[string]$PrivatKey
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
if (Get-Command "winscp"){
# Load Configurations
$Servers = Get-RemoteConnection -Name $Name
if(!(Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.WinSCP.Name -Recurse)){
Write-Host "Copy $($PStemplates.WinSCP.Name) file to the config folder"
Copy-Item -Path $PStemplates.WinSCP.FullName -Destination (Join-Path -Path $PSconfigs.Path -ChildPath $PStemplates.WinSCP.Name)
}
$IniFile = $(Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.WinSCP.Name -Recurse).FullName
foreach($Server in $Servers){
# get port from Protocol
if(!$Port){
$Server.Protocol | %{if($_.Name -eq "ssh" -and $_.Port -ne ""){$Port = $_.Port}}
}
if(!$Port -or $Port -eq 0){
$Port = 22
}
# set servername
$Servername = $Server.Name
# set username
if(!$User){$User = $Server.User}
# set privatkey
if(!$PrivatKey){$PrivatKey = Invoke-Expression ($Command = '"' + $Server.PrivatKey + '"')}
#Set Protocol
if($PrivatKey -eq ""){
Invoke-Expression ("WinSCP scp://$User@$Servername" + ":$Port" + " /ini=$IniFile")
}else{
Invoke-Expression ("WinSCP scp://$User@$Servername" + ":$Port" + " /privatekey='$PrivatKey'" + " /ini=$IniFile")
}
}
}
}